<?php
declare (strict_types = 1);

namespace app\controller\admin;

use think\Request;
use app\model\AdminUserLoginLog;
use app\traits\MyControllerTrait;
use app\model\AdminUser;

class AdminUserLoginLogController
{
    use MyControllerTrait;

    protected string $uri = '/admin/user-login-log';

    public function index(Request $request)
    {
        $data = [
            'uri'   => $this->uri,
            'menus' => $this->getMenuNav($this->uri.'/'.__FUNCTION__),
            'title' => $this->getPermissionTitle($this->uri.'/'.__FUNCTION__),
        ];
        return view($this->uri.'/'.__FUNCTION__, $data);
    }// index() end

    public function show(Request $request)
    {
        $admin_user_account = $request->get('admin_user_account/s', '');// admin_user.account
        $created_at         = $request->get('created_at/s', '');// admin_action_log.created_at

        $page  = $request->get('page/d', 1);
        $limit = $request->get('limit/d', 10);
        $where = "";
        $raw   = [];

        if (strlen($admin_user_account) > 0)
        {
            $admin_user_id = AdminUser::whereRaw("account = :account", ['account' => $admin_user_account])->value('id') ?? 0;
            $where               .= " AND admin_user_id = :admin_user_id";
            $raw['admin_user_id'] = $admin_user_id;
        }
        if (strlen($created_at) > 0)
        {
            $where            .= " AND created_at >= :created_at";
            $raw['created_at'] = $created_at;
        }

        [$total, $data] = AdminUserLoginLog::searchForShow(AdminUserLoginLog::class, $where, $raw, $page, $limit);
        foreach ($data as $key => $val){
            $data[$key]['admin_user_account'] = AdminUser::whereRaw('id = :id', ['id' => $val['admin_user_id']])->value('account') ?? '保密';
        }
        return json(['code' => 0, 'message' => 'success', 'data' => $data, 'total' => $total]);
    }// show() end
}
